using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Templates_MagicNumberCompetition : BaseTemplateUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }

    public override void DataBind()
    {
        Acms.Bll.PostControl ctrlMagicNumber = CurrentPost.FindPostControl("MagicNumber");
        if (ctrlMagicNumber != null)
        {
            Acms.AcmsControls.TextContentControlProperties prop = ctrlMagicNumber.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
            if (prop != null)
            {
                MagicNumber = prop.Text;
            }
        }
        Acms.Bll.PostControl ctrlPrize = CurrentPost.FindPostControl("Prize");
        if (ctrlPrize != null)
        {
            Acms.AcmsControls.TextContentControlProperties prop = ctrlPrize.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
            if (prop != null)
            {
                Prize = prop.Text;
            }
        }
        base.DataBind();
    }

    protected string MagicNumber
    {
        get { return ViewState["MagicNumber"] as string; }
        set { ViewState["MagicNumber"] = value; }
    }

    protected string Prize
    {
        get { return ViewState["Prize"] as string; }
        set { ViewState["Prize"] = value; }
    }
    
    protected void btnEnter_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            TextBox txtNumber = viewDownload.FindControl("txtNumber") as TextBox;
            Panel pnlEnter = viewDownload.FindControl("pnlEnter") as Panel;
            Panel pnlYouWon = viewDownload.FindControl("pnlYouWon") as Panel;
            Panel pnlSorry = viewDownload.FindControl("pnlSorry") as Panel;
            if (MagicNumber != null && txtNumber != null && pnlEnter != null && pnlYouWon != null && pnlSorry != null)
            {
                if (txtNumber.Text.Trim() == MagicNumber.Trim())
                {
                    Acms.Bll.Email email = Acms.Bll.Email.CreateInstance(Page.User.Identity.User, MapPath("~/Emails/MagicNumberEmail.xml"));
                    email.Format("CompetitionName", CurrentPostDisplayName);
                    email.Format("Prize", Prize);
                    email.Send();

                    pnlEnter.Visible = false;
                    pnlYouWon.Visible = true;
                    pnlSorry.Visible = false;
                }
                else
                {
                    pnlEnter.Visible = false;
                    pnlYouWon.Visible = false;
                    pnlSorry.Visible = true;
                }
            }

        }
    }
}
